﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using org.webguvenligi.NSecureImageLib;
using System.Reflection;
using System.Drawing.Imaging;

namespace NSecureImageTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class NSecureImageTest
    {
        public NSecureImageTest()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private static string GetCurrentImagePath()
        {
            return Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", String.Empty);
        }
        private static string GetCurrentExecutionPath(string imageName)
        {
            return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", String.Empty), imageName);
        }
        [TestMethod]
        public void TestImageTypeDetection()
        {
            Assert.IsTrue(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.png").Guid == System.Drawing.Imaging.ImageFormat.Png.Guid);
            Assert.IsTrue(NSecureImage.IsValidImage(GetCurrentImagePath(), "untitled.bmp").Guid == System.Drawing.Imaging.ImageFormat.Bmp.Guid);
            Assert.IsTrue(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.jpg").Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid);            
        }
        [TestMethod]
        public void TestImageTypeCompare()
        {
            Assert.IsTrue(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.png", ImageFormat.Png));
            Assert.IsTrue(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.gif", ImageFormat.Gif));
            Assert.IsTrue(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.jpg", ImageFormat.Jpeg));
        }
        [TestMethod]
        public void TestImageTypeNotMatch()
        {
            Assert.IsFalse(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.gif", ImageFormat.Bmp));
            Assert.IsFalse(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.jpg", ImageFormat.Png));
            Assert.IsFalse(NSecureImage.IsValidImage(GetCurrentImagePath(), "valid.png", ImageFormat.Icon));
        }
        [TestMethod]
        [ExpectedException(typeof(org.webguvenligi.NSecureImageLib.NSecureImageException))]
        public void TestImageTypeExceptions()
        {
            NSecureImage.IsValidImage(GetCurrentImagePath(),"test.gif",ImageFormat.Gif);
        }
    }
}
